package com.jdojo.print;

/**
 * @author zhufeifei
 * @date 2024-04-24 15:53
 */

import javafx.application.Application;
import javafx.collections.ObservableSet;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Objects;

public class PrintingNodes extends Application {
    private Label jobStatus = new Label();

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        ObservableSet<Printer> allPrinters = Printer.getAllPrinters();
        Printer printer = null;
        for (Printer p : allPrinters) {
            System.out.println(p.getName());
            if (Objects.equals("Microsoft Print to PDF", p.getName())) {
                printer = p;
            }
        }

        VBox root = new VBox(5);
        Label textLbl = new Label("Text:");
        TextArea text = new TextArea();
        text.setPrefRowCount(10);
        text.setPrefColumnCount(20);
        text.setWrapText(true);
// Button to print the TextArea node
        Button printTextBtn = new Button("Print Text");
        Printer finalPrinter = printer;
        printTextBtn.setOnAction(e -> print(text, finalPrinter));
// Button to print the entire scene
        Button printSceneBtn = new Button("Print Scene");
        printSceneBtn.setOnAction(e -> print(root, finalPrinter));
        HBox jobStatusBox = new HBox(5, new Label("Print Job Status:"), jobStatus);
        HBox buttonBox = new HBox(5, printTextBtn, printSceneBtn);
        root.getChildren().addAll(textLbl, text, jobStatusBox, buttonBox);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Printing Nodes");
        stage.show();
    }

    private void print(Node node, Printer printer) {
        jobStatus.textProperty().unbind();
        jobStatus.setText("Creating a printer job...");
// Create a printer job for the default printer
        PrinterJob job = PrinterJob.createPrinterJob(printer);
        if (job != null) {
// Show the printer job status
            jobStatus.textProperty().bind(job.jobStatusProperty().asString());
// Print the node
            boolean printed = job.printPage(node);
            if (printed) {
// End the printer job
                job.endJob();
            } else {
                jobStatus.textProperty().unbind();
                jobStatus.setText("Printing failed.");
            }
        } else {
            jobStatus.setText("Could not create a printer job.");
        }
    }
}
